/*-----------------------------------------------------------------------------
 Decoration
-----------------------------------------------------------------------------*/
$(document).ready(function() {
    //Hover states on the static widgets
    $(document).on("mouseover", ".ui-state-default", function(){ 
        $(this).addClass("ui-state-hover");
    });
    $(document).on("mouseout", ".ui-state-default", function(){ 
        $(this).removeClass("ui-state-hover");
    });
    
    //When hover Icons in Gridview
    $(document).on("mouseover", ".haveIconHover", function(){ 
        $(this).addClass("ui-icon-my-hover");
    });
    $(document).on("mouseout", ".haveIconHover", function(){ 
        $(this).removeClass("ui-icon-my-hover");
    });
});

/*-----------------------------------------------------------------------------
 Show messages
-----------------------------------------------------------------------------*/
function showMessage(container, msg)
{
  $(container).html(msg).show();

  setTimeout(function() {
    $(container).hide();
  }, 5000);
}

/*-----------------------------------------------------------------------------
 Convert title to title-seo
-----------------------------------------------------------------------------*/
jQuery.fn.convertSeoStr = function() {
    var o = $(this[0]); // It's your element
    var args = arguments[0] || {}; // It's your object of arguments

    var separator = args.separator;
    var titleSeo =  o.text().toLowerCase();
    var charReplace, charFind;
    var tArray = new Array();
    
    var unicode = [
        ['a', 'd', 'e', 'i', 'o', 'u', 'y'],
        ['á', 'à', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ặ', 'ằ', 'ẳ', 'ẵ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ'],
        ['đ'],
        ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ'],
        ['í', 'ì', 'ỉ', 'ĩ', 'ị'],
        ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ'],
        ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự'],
        ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'],
    ];
    
    for (i=0; i<unicode[0].length; i++) {
        charReplace = unicode[0][i];
        tArray = unicode[i+1];
        
        for(j=0; j<tArray.length; j++) {
            charFind = unicode[i+1][j];
            titleSeo = titleSeo.replace(new RegExp(charFind, 'g'), charReplace);
        }
    }
    
    //special characteres   
    titleSeo = titleSeo.replace(/([.,:;+\-\*\/=\(\)\{\}\[\]><~!@#$%^&?'\\`"])/g, '');
    titleSeo = titleSeo.replace(/\ /g, separator);
    titleSeo = titleSeo.replace(new RegExp('^\\' + separator + '|\\' + separator + '$', 'g'), '');
    
    return titleSeo;
};

/*-----------------------------------------------------------------------------
 Alert and Confirm message via Dialog
-----------------------------------------------------------------------------*/
(function( $ ){
    $.fn.showLoading = function(params) {
        if ((params.title === undefined) || params.title == '') {
            params['title'] = 'Loading...';
        }

        $(this).attr('title', params['title']).html(params.msg).dialog({
            resizable: false,
            draggable: false,
            closeText: '',
            modal: true,
            minWidth: 450,
            minHeight: 100,
            maxHeight: 200,
            position: ['center', 100],
            dialogClass: 'dialogLoading'
        });
    }

    $.fn.showAlert = function(params, callback) {
        if ((params.title === undefined) || params.title === '') {
            params['title'] = 'Alert';
        }

        $(this).attr('title', params['title']).html(params.msg).dialog({
            resizable: false,
            draggable: false,
            closeText: '',
            modal: true,
            minWidth: 450,
            maxHeight: 200,
            position: ['center', 100],
            buttons: {
                OK: function() {
                    $( this ).dialog( "close" );
                    if (typeof callback == "function") callback();
                }
            }
        });
    }
    
    $.fn.showConfirm = function(params, f1, f2) {
        if ((params.title === undefined) || params.title == '') {
            params['title'] = 'Notification';
        }

        $(this).attr('title', params['title']).html(params.msg).dialog({
            resizable: false,
            draggable: false,
            closeText: '',
            modal: true,
            minWidth: 450,
            maxHeight: 200,
            position: ['center', 100],
            buttons: {
                No: function() {
                    $( this ).dialog( "close" );
                    if (typeof f2 == "function") f2();
                },
                Yes: function() {
                    $( this ).dialog( "close" );
                    if (typeof f1 == "function") f1();
                }
            }
        });
    }
})( jQuery );

/*-----------------------------------------------------------------------------
 Helper
-----------------------------------------------------------------------------*/
function clearTextField(id) {
    $('#' + id).val('');
}